package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.controller.info.AccountInfo;
import com.woniuxy.model.VoteAccount;
import com.woniuxy.service.VoteAccountService;
import com.woniuxy.util.WoniuxyBeanCopyUtil;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class AccountDetailServiceImpl implements UserDetailsService {

    @Resource
    private VoteAccountService voteAccountService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println(username);
        VoteAccount user = voteAccountService.getOne(new QueryWrapper<VoteAccount>()
        .eq("account_name",username));
        System.out.println(user);
        // 找不到用户
        if(user==null){
            throw new UsernameNotFoundException("账号不存在");
        }
        // 找到用户，
        return WoniuxyBeanCopyUtil.copyOne(user, AccountInfo.class);
    }
}
